package com.demo.function.dataauth;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.dao.mapper.UserMapper;
import com.demo.function.dataauth.annotation.DataAuth;
import com.demo.model.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class DataAuthService {

    @Resource
    private UserMapper userMapper;

    @DataAuth(tableName = "user", columnName = "user_name")
    public List<User> query() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserId, 11L);
        return userMapper.selectList(wrapper);
    }

    @DataAuth(tableName = "user", columnName = "user_name")
    public Page<User> queryByPage(int current, int pageSize) {
        Page<User> page = new Page<>(current, pageSize);

        Page<User> p = userMapper.selectPage(page, null);
        log.info(JSON.toJSONString(p.getRecords()));
        return p;
    }

    @DataAuth(tableName = "user", columnName = "user_name")
    public List<User> queryByUserIdBetween(long min, long max) {
        List<User> users = userMapper.queryByUserIdBetween(min, max);
        log.info(JSON.toJSONString(users));
        return users;
    }
}
